import React, { useState, useEffect } from 'react';
import { connect } from 'dva';
import PropTypes from 'prop-types';
import { Table, Popconfirm, Button, Space } from 'antd';

const columns = [
  {
    title: '日期',
    dataIndex: 'messageTs',
  },
  {
    title: '标题',
    dataIndex: 'title',
  }
];

const AdminMessageList = ({ dispatch, messageUserid, messageList }) => {

  useEffect(() => {
    dispatch({
      type: 'message/getMessageByUserid',
      payload: { messageUserid },
    });
  })

  const [selectedRowKeys, setSelectedRowKeys] = useState([]);

  const hasSelected = selectedRowKeys.length > 0;

  const rowSelection = {
    selectedRowKeys,
    onChange: setSelectedRowKeys,
  };

  function handleBack() {
    dispatch({
      type: 'message/setMessageState',
      payload: {
        AdminMessageListState: 'statuslist',
      },
    });
  }

  return (
    <div>
      <div>
        <div style={{ marginBottom: 16 }}>
          <Button type="primary" onClick={() => {}} disabled={!hasSelected}>
            删除
          </Button>
          <Button type="primary" onClick={handleBack} style={{ marginLeft: 20 }}>
            返回
          </Button>
          <span style={{ marginLeft: 8 }}>
            {hasSelected ? `选择了${selectedRowKeys.length}条` : ''}
          </span>
        </div>
        <Table rowSelection={rowSelection}
          columns={columns}
          dataSource={messageList}
          onRow={item => {
            return {
              onClick: () => {
                const { messageId } = item;
                dispatch({
                  type: 'message/setMessageState',
                  payload: {
                    messageId,
                    AdminMessageListState: 'checkmessage'
                  },
                });
              }
            }
          }} />
      </div>
    </div>
  );
};

export default connect(state => {
  const { messageUserid, messageList } = state.message;
  messageList.filter(item => item.replyId);
  return {
    messageUserid,
    messageList,
  }
})(AdminMessageList);

